﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.UserPermission
{
    public partial class DeptEdit : System.Web.UI.Page
    {
        UserPermissionBll userPermissionBll = new UserPermissionBll();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                UserPermissionBll userPermissionBll = new UserPermissionBll();
                if (!userPermissionBll.HasPermission(CurrentInfo.Instance.CurrentUser.ID, (int)Permission.DeptManager))
                {
                    Response.Write("您没有权限使用该功能");
                    Response.End();
                    return;
                }
                InitTreeView();
            }
        }

        private void InitTreeView()
        {
            tvDept.Nodes.Clear();
            TreeNode node = new TreeNode();
            node.Value = "0";
            node.Text = "部门";
            node.Target = "0";
            tvDept.Nodes.Add(node);

            DataTable dataTable = userPermissionBll.GetDeptDataTable();
            InitTreeNode(dataTable, "0", node);
            tvDept.Nodes[0].Selected = true;
            tvDept.ExpandAll();

            tvDept_SelectedNodeChanged(null, null);
        }

        private void InitTreeNode(DataTable dataTable, string deptID, TreeNode treeNode)
        {
            DataRow[] dataRows = dataTable.Select(string.Format("ParentDeptID={0}", deptID));
            foreach (DataRow dataRow in dataRows)
            {
                TreeNode node = new TreeNode();
                node.Text = dataRow["DeptName"].ToString();
                node.Value = dataRow["DeptID"].ToString();
                node.Target = dataRow["DeptLevel"].ToString();

                treeNode.ChildNodes.Add(node);
                InitTreeNode(dataTable, node.Value, node);
            }
        }

        protected void tvDept_SelectedNodeChanged(object sender, EventArgs e)
        { 
            //BindChileDept();
            TreeNode node = tvDept.SelectedNode;
            if (!node.Value.Equals("0"))
            {
                lblSelectDept.Text = node.Text;
                txtDeptName.Text = node.Text;
            }
            else
            {
                lblSelectDept.Text = node.Text;
                txtDeptName.Text = string.Empty;
            }


            if (node.Target.Equals("3"))
            {
                btnAdd.Enabled = false;
            }
            else
            {
                btnAdd.Enabled = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sql = "UPDATE dbo.t5715_Department SET DeptName = @DeptName WHERE DeptID = @DeptID";
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@DeptName", DbType.String, txtDeptName.Text.Trim());
            database.AddInParameter(dbCommand, "@DeptID", DbType.String, tvDept.SelectedNode.Value);
            database.ExecuteNonQuery(dbCommand);

            InitTreeView();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            TreeNode node = tvDept.SelectedNode;
            if(node == null)
            {
                return;
            }

            string deptLevel = string.Empty;
            switch (node.Target)
            { 
                case "0" :
                    deptLevel = "1";
                    break;
                case "1" :
                    deptLevel = "2";
                    break;
                case "2" :
                    deptLevel = "3";
                    break;
            }

            if (string.IsNullOrWhiteSpace(deptLevel))
            {
                return;
            }

            string sql = @"INSERT INTO dbo.t5715_Department
                                ([DeptName]
                                ,[ParentDeptID]
                                ,[DeptLevel])
                            VALUES
                                (@DeptName
                                ,@ParentDeptID
                                ,@DeptLevel)";
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@DeptName", DbType.String, txtDeptName.Text.Trim());
            database.AddInParameter(dbCommand, "@ParentDeptID", DbType.String, node.Value);
            database.AddInParameter(dbCommand, "@DeptLevel", DbType.String, deptLevel);
            database.ExecuteNonQuery(dbCommand);

            InitTreeView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string sql = "DELETE FROM dbo.t5715_Department WHERE DeptID = @DeptID";
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@DeptID", DbType.String, tvDept.SelectedNode.Value);
            database.ExecuteNonQuery(dbCommand);

            InitTreeView();
        }
    }
}